package org.zsk.cfl.controller;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.flowable.bpmn.model.Process;
import org.zsk.cfl.exception.BizException;
import org.zsk.cfl.response.ResultBody;
import org.zsk.cfl.service.IFlowService;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * </p>
 *
 * @author zsk
 * @date 2020/10/20 上午10:40
 */
@RestController
@Slf4j
public class IFlowController {

    @Autowired
    private IFlowService flowService;

    @RequestMapping("/create")
    @SuppressWarnings("unchecked")
    public ResultBody createFlow(String bpmnXMLName, String deploymentName) {
        String flowPath = "/home/zsk13/IdeaProjects/flowable-lv-proj/csdn-flowable-lv/src/main/resources/" + bpmnXMLName;

        Map<String, Object> createRes = Optional
                .ofNullable(flowService.createFlow(flowPath, deploymentName))
                .orElseThrow(() -> new BizException("创建流程失败"));

        List<Process> processes = (List<Process>) Optional.
                ofNullable(createRes.get("processes"))
                .orElseThrow(() -> new BizException("获取流程信息失败"));

        List<String> processKeys = processes.stream().map(Process::getId).collect(Collectors.toList());

        Map<String, Object> data = new HashMap<>(2);
        data.put("processKeys", processKeys);
        data.put("deployId", ((Deployment) createRes.get("deployment")).getId());
        return ResultBody.success(data);
    }

    @RequestMapping("/start")
    public ResultBody startFlow(@RequestBody @RequestParam(required = false) Map<String, Object> paras) {
        if (CollectionUtils.isEmpty(paras)) {
            return ResultBody.error("启动流程失败");
        }

        String processId = (String) paras.get("processId");
        if (StringUtils.isEmpty(processId)) {
            return ResultBody.error("启动流程失败");
        }

        String assigneeList=(String)paras.get("assignees");
        if (!StringUtils.isEmpty(assigneeList)){
            List<String> assignees=Arrays.asList(assigneeList.split(","));
            paras.put("assignees",assignees);
        }

        ProcessInstance processInstance = Optional
                .ofNullable(flowService.startFlow(processId, paras))
                .orElseThrow(() -> new BizException("创建流程失败"));

        Map<String, String> data = new HashMap<>(1);
        data.put("processId", processInstance.getId());
        return ResultBody.success(data);
    }

    @RequestMapping(value = "processDiagram")
    public ResultBody genProcessDiagram(HttpServletResponse httpServletResponse, String processId) {
        flowService.genProcessDiagram(httpServletResponse, processId);
        return ResultBody.success();
    }

    @RequestMapping("/accept")
    public ResultBody acceptTask(@RequestBody @RequestParam(required = false) Map<String, String> paras) {
        if (CollectionUtils.isEmpty(paras)) {
            return ResultBody.error("请输入任务参数");
        }

        String taskId = paras.get("taskId");
        if (StringUtils.isEmpty(taskId)) {
            return ResultBody.error("请输入任务ID");
        }

        Map<String, Object> flowParas = new HashMap<>(paras);
        flowParas.put("outcome", "通过");
        flowService.completeTask(taskId, flowParas);
        return ResultBody.success();
    }

    @RequestMapping("/complete")
    public ResultBody completeTask(@RequestBody @RequestParam(required = false) Map<String, Object> paras) {
        if (CollectionUtils.isEmpty(paras)) {
            return ResultBody.error("请输入任务参数");
        }

        String taskId = (String)paras.get("taskId");
        if (StringUtils.isEmpty(taskId)) {
            return ResultBody.error("请输入任务ID");
        }
        String pers=(String)paras.get("pers");
        if (!StringUtils.isEmpty(pers)){
            List<String> persList=Arrays.asList(pers.split(","));
            paras.put("pers",persList);
        }
        String assigneeList=(String)paras.get("assignees");
        if (!StringUtils.isEmpty(assigneeList)){
            List<String> assignees=Arrays.asList(assigneeList.split(","));
            paras.put("assignees",assignees);
        }

        Map<String, Object> flowParas = new HashMap<>(paras);
        flowService.completeTask(taskId, flowParas);

        Map<String, String> data = new HashMap<>();
        data.put("taskId", taskId);

        return ResultBody.success(data);
    }

    @RequestMapping("/reject")
    public ResultBody rejectTask(@RequestBody @RequestParam(required = false) Map<String, String> paras) {
        if (CollectionUtils.isEmpty(paras)) {
            return ResultBody.error("请输入任务参数");
        }

        String taskId = paras.get("taskId");
        if (StringUtils.isEmpty(taskId)) {
            return ResultBody.error("请输入任务ID");
        }

        Map<String, Object> flowParas = new HashMap<>(paras);
        flowParas.put("outcome", "拒绝");
        flowService.completeTask(taskId, flowParas);

        Map<String, String> data = new HashMap<>();
        data.put("taskId", taskId);
        return ResultBody.success(data);
    }


}
